import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter_demo/models/common_model.dart';
import 'package:flutter_demo/models/home_model.dart';
import 'package:flutter_demo/widget/LoadingContainer.dart';
import 'package:flutter_demo/widget/XyLocalBoNav.dart';
import 'package:flutter_demo/widget/XyLocalNav.dart';
import 'package:flutter_demo/widget/webview.dart';
import 'package:flutter_swiper/flutter_swiper.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class XyHomePage extends StatefulWidget {
  @override
  _XyHomePageState createState() => _XyHomePageState();
}




class _XyHomePageState extends State<XyHomePage> {

  bool _isLoading = true;
  List<CommonModel> _bannerList = [];

  @override
  void initState() {
    super.initState();
    _handlerPage();
  }

  @override
  Widget build(BuildContext context) {
    // 初始化插件
    ScreenUtil.init(context, width: 750, height: 1334, allowFontScaling: false);
    return Scaffold(
      backgroundColor: Color(0xFFF5F5F5),
      body: LoadingContainer(
        child: Stack(
          children: <Widget>[
            MediaQuery.removePadding(
                removeTop: true,
                context: context,
                child: NotificationListener(
                  onNotification: (scrollNotification) {
                    return null;
                  },
                  child: RefreshIndicator(
                      child: ListView(
                        children: <Widget>[
                          Container(
                              height: ScreenUtil().setHeight(604),
                              child: Stack(
                                children: <Widget>[
                                  Container(
                                    height: ScreenUtil().setHeight(500),
                                    width: MediaQuery.of(context).size.width,
                                    child: Swiper(
                                      itemCount: _bannerList.length,
                                      autoplay: true,
                                      itemBuilder: (BuildContext context, int index) {
                                        return GestureDetector(
                                          onTap: () {
                                            CommonModel model = _bannerList[index];
                                            Navigator.push(
                                              context,
                                              MaterialPageRoute(
                                                builder: (context) => WebView(
                                                    url: model.url
                                                ),
                                              ),
                                            );
                                          },
                                          child: Image.network(
                                            _bannerList[index].icon,
                                            fit: BoxFit.fill,
                                          ),
                                        );
                                      },
                                      pagination: new SwiperPagination(),
                                    ),
                                  ),
                                  Positioned(
                                    top: ScreenUtil().setHeight(400),
                                    child: Container(
                                      width: MediaQuery.of(context).size.width,
                                      padding: EdgeInsets.fromLTRB(ScreenUtil().setWidth(32), 0, ScreenUtil().setWidth(32), 0),
                                      child: XyLocalNav()
                                    ),
                                  )
                                ],
                              )
                          ),
                          Container(
                            width: MediaQuery.of(context).size.width,
                            padding: EdgeInsets.fromLTRB(ScreenUtil().setWidth(32), 10, ScreenUtil().setWidth(32), 0),
                            child: Row(
                                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                children: <Widget>[
                                  XyLocalBoNav(topName: '智能收款', bottomName: '稳定 灵活 安全',),
                                  SizedBox(
                                    width: ScreenUtil().setWidth(20),
                                  ),
                                  XyLocalBoNav(topName: '智能还款', bottomName: '智能规划 AI还款',),
                                ],
                            ),
                          )
                        ],
                      ),
                      onRefresh: () {
                        return null;
                      }
                  ),
                )
            ),
          ],
        ), loading: _isLoading, cover: false,),
//      appBar: Container(
//        child: AppBar(
//          backgroundColor: Colors.transparent,
//
//        ),
//      ),
    );
  }

  // 获取首页信息的方法
  Future<Null> _handlerPage() async {
    Future<String> loadString = DefaultAssetBundle.of(context).loadString("jsons/homeJson.json");
    loadString.then((data) {
      Utf8Decoder utf8decoder = Utf8Decoder(); // fix 中文乱码
      var result = json.decode(data);
      HomeModel model = HomeModel.fromJson(jsonDecode(data));
      setState(() {
        _bannerList = model.bannerList;
//        localNavList = model.localNavList;
//        subNavList = model.subNavList;
//        salesBoxModel = model.salesBox;
        _isLoading = false;
      });
    });
  }
}


